package gg.com.toolbox.util;

import android.app.Activity;
import android.app.Application;

import java.util.LinkedList;
import java.util.List;

/**
 * 公共的数据信息
 * Created by gaoge on 2017/10/10.
 */

public class MyApplication extends Application {
    //保存activity的集合
    private List<Activity> activityList = new LinkedList<Activity>();
    //单例对象
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    /**
     * 单例模式中获取唯一的MyApplication实例
     */
    public static MyApplication getInstance() {
        if (null == instance) {
            instance = new MyApplication();
        }
        return instance;
    }

    /**
     * 添加Activity到容器中
     */
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    /**
     * 从容器中删除Activity
     */
    public void deleteActivity(Activity activity) {
        if (activityList != null && activityList.size() > 0) {
            for (int i = 0; i < activityList.size(); i++) {
                if (activityList.get(i) == activity) {
                    activityList.remove(i);
                }
            }
        }
    }

    /**
     * 关闭除开指定Activity外所有Activity
     */
    public void closeActivity(Activity activity) {
        if (activityList != null && activityList.size() > 0) {
            for (int i = 0; i < activityList.size(); i++) {
                if (activityList.get(i) != activity) {
                    activityList.get(i).finish();
                }
            }
        }
    }

    /**
     * 退出应用
     */
    public void logout() {
        if (activityList != null && activityList.size() > 0) {
            for (int i = 0; i < activityList.size(); i++) {
                    activityList.get(i).finish();
            }
        }
    }
}
